<?php


class Goods_normalApp extends BackendApp
{
	var $_goodsnormal_mod;
	var $_goodsnormalsub_mod;


	function __construct()
	{
		$this->Goods_normalApp();

	}

	function Goods_normalApp()
	{
		parent::BackendApp();
		$this->_goodsnormal_mod =& m('goodsnormal');
		$this->_goodsnormalsub_mod =& m('goodsnormalsub');
	}

	function index()
	{
		$conditions = "1=1 ";
		if($_GET['g_code']!=''){
			$conditions.=" and g_code like '%".$_GET['g_code']."%'";
		}
		if($_GET['g_name']!=''){
			$conditions.=" and g_name like '%".$_GET['g_name']."%'";
		}
		if($_GET['g_owner']!=''){
			$conditions.=" and g_owner like '%".$_GET['g_owner']."%'";
		}
		$user_ids = $this->checkpower();
		if(!empty($user_ids)){
			$conditions.=" and g_optid in(".$user_ids.")";
		}
		$page   =   $this->_get_page(10);   //获取分页信息

		$sql="SELECT n.*,m.user_name ,IFNULL(x.ws_tid,0) as ws_tid FROM zys_goods_normal n
		LEFT JOIN zys_member m ON n.g_optid=m.user_id
		LEFT JOIN (SELECT ws_tid,IFNULL(ws_gid,0) as ws_gid from zys_waybill_sub_normal GROUP BY ws_gid) x on n.g_tid=ws_gid
		 where 
		 $conditions AND n.g_type='整车'  ORDER BY g_opdate DESC limit $page[limit]";
		 $goodsnormal=$this->_goodsnormal_mod->getAll($sql);
		 $sql="select count(1) from zys_goods_normal where $conditions AND g_type='整车货单'";
		 $page['item_count']=$this->_goodsnormal_mod->getOne($sql);   //获取统计数据
		 $this->_format_page($page);
		 $this->assign('page_info', $page);   //将分页信息传递给视图，用于形成分页条
		 $this->assign('auth', $goodsnormal);
		 $this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
                                      'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
		 $this->assign('tabself', json_encode(array('text'=>'货单管理')));
		 $this->display('goods_normal.index.html');
	}
	//新增
	function add(){
		if (!IS_POST){
			$goodsnormal=array();
			$goodsnormal['g_code']= 'T'.gmtime();
			$goodsnormal['user_name']=$this->visitor->get('user_name');
			$this->assign('auth', $goodsnormal);
			$db=&db();
			$sql3="SELECT d.d_value  FROM zys_dictionary d  WHERE d.d_code='g_cartype'";
			$g_cartypes=$db->getall($sql3);
			$type= array();
			foreach ($g_cartypes as $value) {
				$type[$value['d_value']]=$value['d_value'];
			}
			$this->assign('g_cartypes',$type);
			/* 导入jQuery的表单验证插件 */
			$this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js,jquery.plugins/jquery.validate.js',
        	'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
			$this->assign('isbool',1);//等于1显示提交按钮等等
			$this->display('goods_normal.form.html');
		}else{
			$data = array(
				'g_unloading_date'  =>  gmstr2time($_POST['g_unloading_date']),
				'g_loading_date'  =>  gmstr2time($_POST['g_loading_date']),
				//'g_rdate'  =>  gmstr2time($_POST['g_rdate']),
				'g_loading_lng'  =>  $_POST['g_loading_lng'],
				'g_loading_lat'  =>  $_POST['g_loading_lat'],
				'g_unloading_lng'  =>  $_POST['g_unloading_lng'],
				'g_unloading_lat'  =>  $_POST['g_unloading_lat'],
			
				//'g_tgdate'  =>  gmstr2time($_POST['g_tgdate']),
				'g_cartype'  =>  $_POST['g_cartype'],
                'g_code'  => $_POST['g_code'],
                'g_owner'  => $_POST['hzshow'],
			  	'g_ownertid'  =>  $_POST['g_ownertid'],
                'g_owner_tel' => $_POST['g_owner_tel'],
                'g_loading_place'	     => $_POST['g_loading_place'],
			    'g_unloading_place'      => $_POST['g_unloading_place'],
			    'g_type'   => '整车',
				'g_consignee' => $_POST['g_consignee'],
                'g_consignee_tel'	     => $_POST['g_consignee_tel'],
			    'g_consignor'      => $_POST['g_consignor'],

			    'g_consignor_tel'	     => $_POST['g_consignor_tel'],
			    'g_note'      => $_POST['g_note'],

			    'g_optid'      => $this->visitor->get('user_id'),
			    'g_opdate'	     => gmtime()
			);
			/* 保存 */
			$rows = $this->_goodsnormal_mod->add($data);

			if ($this->_goodsnormal_mod->has_error())
			{
				$this->show_warning($this->_goodsnormal_mod->get_error(), '',   'index.php?app=goods_normal&act=add');
				return;
			}
			$data2= array(
                'gs_gtid'  => $rows,
                'gs_name'  => '整车',
                'gs_piece' => '1',
                'gs_weight'	     => '1',
			    'gs_size'      => '1',
			    'g_consignor'   => $_POST['g_ownertid'],
			    'g_carrier'   => $_POST['operator'],
			);
			$rows2 = $this->_goodsnormalsub_mod->add($data2);
			if ($this->_goodsnormalsub_mod->has_error())
			{
				$this->show_warning($this->_goodsnormalsub_mod->get_error(), '',   'index.php?app=goods_normal&act=edit&id=' . $rows);
				return;
			}
			$userid = $this->visitor->get('user_id');
			$unit = $this->visitor->get('unit');
			$num = $this->checkaddr($_POST['g_loading_place'],$userid,$unit);
			if($num==0){
				$loading = array(
					'user_id'    	=>	$userid,
					'consignee' 	=>	$_POST['g_consignor'],
					'address' 		=>	$_POST['g_loading_place'],
					'phone_tel'	=>	$_POST['g_consignor_tel'],
					'lng'			=>	$_POST['g_loading_lng'],
					'lat'			=>	$_POST['g_loading_lat'],
					'addrtype'		=>	'整车装货',
					'unit'			=>	$unit,
				);
				$this->addaddr($loading);
			}
			$num1 = $this->checkaddr($_POST['g_unloading_place'],$userid,$unit);
			if($num1==0){
				$unloading = array(
					'user_id'    	=>	$userid,
					'consignee' 	=>	$_POST['g_consignee'],
					'address' 		=>	$_POST['g_unloading_place'],
					'phone_tel'	=>	$_POST['g_consignee_tel'],
					'lng'			=>	$_POST['g_unloading_lng'],
					'lat'			=>	$_POST['g_unloading_lat'],
					'addrtype'		=>	'整车卸货',
					'unit'			=>	$unit,
				);
				$this->addaddr($unloading);
			}

			$this->show_message('添加成功',
                '返回货单',   'index.php?app=goods_normal&act=edit&id=' . $rows
			);
		}
	}
	//编辑
	function edit()
	{

		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
         $db=&db();
		if (!IS_POST){
			$conditions=" 1 =1 and g_tid=".$id;
			$sql="
			SELECT n.*,m.user_name,o.o_name FROM zys_goods_normal n
LEFT JOIN zys_member m ON n.g_optid=m.user_id
LEFT JOIN zys_operator o ON n.g_ownertid=o.o_tid 
			where $conditions ";
			$goodsnormal=$this->_goodsnormal_mod->getRow($sql);
			$this->assign('id', $id);
			$this->assign('isbool',1);//等于1显示提交按钮等等
			$this->assign('auth', $goodsnormal);
			$conditions2=" 1=1 and gs_gtid=".$id;
			$sql2="SELECT n.*,o.o_name FROM zys_goods_normal_sub n
			LEFT JOIN zys_operator o ON n.g_consignor=o.o_tid 
			where $conditions2";
			$goodstrunk_normal_sub=$db->getall($sql2);

			foreach ($goodstrunk_normal_sub as $value) {
				
			$sql4="SELECT w.*  FROM zys_waybill_sub_normal w WHERE w.ws_gstid=".$value['gs_tid'];
				$boolarr=$db->getrow($sql4);   //判断是否在普通运单下
				if($boolarr){
					$this->assign('isbool', 0);
				}
			}
			$this->assign('id', $id);
			$sql3="SELECT d.d_value  FROM zys_dictionary d
            WHERE d.d_code='g_cartype'
			";
			$g_cartypes=$db->getall($sql3);  //车类型
			$type= array();
			foreach ($g_cartypes as $value) {
				$type[$value['d_value']]=$value['d_value'];
			}
			$this->assign('g_cartypes',$type);
			$sql5="SELECT g.*,o.o_name  FROM zys_goods_normal_sub g
              LEFT JOIN  zys_operator o ON g.g_carrier=o.o_tid
            WHERE g.gs_gtid=".$id;
				$goods_normal_subrow=$db->getrow($sql5);
			$this->assign('g_consignor', $goods_normal_subrow['o_name']);
			$this->assign('gs_gtid', $goods_normal_subrow['g_consignor']);
		/* 导入jQuery的表单验证插件 */
			$this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js,jquery.plugins/jquery.validate.js',
        	'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
			$this->assign('tabself', json_encode(array('text'=>'货单编辑')));
			$this->assign('tabback', json_encode(array('text'=>'货单管理','url'=>'')));
			$this->display('goods_normal.form.html');
		}else
		{
			$data = array(
				//'g_tgdate'  =>  gmstr2time($_POST['g_tgdate']),
				'g_cartype'  =>  $_POST['g_cartype'],

				'g_unloading_date'  =>  gmstr2time($_POST['g_unloading_date']),
				'g_loading_date'  =>  gmstr2time($_POST['g_loading_date']),
				//'g_rdate'  =>  gmstr2time($_POST['g_rdate']),
				'g_loading_lng'  =>  $_POST['g_loading_lng'],
				'g_loading_lat'  =>  $_POST['g_loading_lat'],
				'g_unloading_lng'  =>  $_POST['g_unloading_lng'],
				'g_unloading_lat'  =>  $_POST['g_unloading_lat'],
			
			
                'g_code'  => $_POST['g_code'],
                'g_ownertid'  => $_POST['g_ownertid'],
				'g_owner'   => $_POST['hzshow'],
                'g_owner_tel' => $_POST['g_owner_tel'],
                'g_loading_place'	     => $_POST['g_loading_place'],
			    'g_unloading_place'      => $_POST['g_unloading_place'],
			    'g_type'   => '整车',
				'g_consignee' => $_POST['g_consignee'],
                'g_consignee_tel'	     => $_POST['g_consignee_tel'],
			    'g_consignor'      => $_POST['g_consignor'],

			    'g_consignor_tel'	     => $_POST['g_consignor_tel'],
			    'g_note'      => $_POST['g_note'],

			    'g_optid'      => $this->visitor->get('user_id'),
				
			);
			$userid = $this->visitor->get('user_id');
			$unit = $this->visitor->get('unit');
			$num = $this->checkaddr($_POST['g_loading_place'],$userid,$unit);
			if($num==0){
				$loading = array(
					'user_id'    	=>	$userid,
					'consignee' 	=>	$_POST['g_consignor'],
					'address' 		=>	$_POST['g_loading_place'],
					'phone_tel'	=>	$_POST['g_consignor_tel'],
					'lng'			=>	$_POST['g_loading_lng'],
					'lat'			=>	$_POST['g_loading_lat'],
					'addrtype'		=>	'整车装货',
					'unit'			=>	$unit,
				);
				$this->addaddr($loading);
			}
			$num1 = $this->checkaddr($_POST['g_unloading_place'],$userid,$unit);
			if($num1==0){
				$unloading = array(
					'user_id'    	=>	$userid,
					'consignee' 	=>	$_POST['g_consignee'],
					'address' 		=>	$_POST['g_unloading_place'],
					'phone_tel'	=>	$_POST['g_consignee_tel'],
					'lng'			=>	$_POST['g_unloading_lng'],
					'lat'			=>	$_POST['g_unloading_lat'],
					'addrtype'		=>	'整车卸货',
					'unit'			=>	$unit,
				);
				$this->addaddr($unloading);
			}
			/* 保存 */
			$rows = $this->_goodsnormal_mod->edit($id, $data);

			if ($this->_goodsnormal_mod->has_error())
			{
				$this->show_warning($this->_goodsnormal_mod->get_error(),   '',   'index.php?app=goods_normal&act=edit&id=' . $id);
				return;
			}
			
			$sql4="SELECT g.*  FROM zys_goods_normal_sub g
            WHERE g.gs_gtid=".$id;
				$goods_normal_subrow=$db->getrow($sql4);
				$data2= array(
			    'g_carrier'   => $_POST['operator'],
			);
			$rows2 = $this->_goodsnormalsub_mod->edit($goods_normal_subrow['gs_tid'], $data2);

			if ($this->_goodsnormalsub_mod->has_error())
			{
				$this->show_warning($this->_goodsnormalsub_mod->get_error(),   '',   'index.php?app=goods_normal&act=edit&id=' . $id);
				return;
			}
			$this->show_message('编辑成功',
                '继续编辑',   'index.php?app=goods_normal&act=edit&id=' . $id
			);
		}
 
	}
	//删除
	function drop(){
		$arr = isset($_GET['id']) ? trim($_GET['id']) : '';
		$db=&db();
		$arrs=explode(',', $arr);
		if($arrs!=null&&count($arrs)>0){
			foreach ($arrs as $v){

				$sql="SELECT n.*,m.user_name ,IFNULL(x.ws_tid,0) as ws_tid FROM zys_goods_normal n
		LEFT JOIN zys_member m ON n.g_optid=m.user_id
		LEFT JOIN (SELECT ws_tid,IFNULL(ws_gid,0) as ws_gid from zys_waybill_sub_normal GROUP BY ws_gid) x on n.g_tid=ws_gid WHERE g_tid=".$v;
				$status=$db->getRow($sql);
				if($status&&$status['ws_tid']){

				}else{
					if (!$this->_goodsnormal_mod->drop($v)){
						$this->show_warning($this->_goodsnormal_mod->get_error()     ,   '',   'index.php?app=goods_normal');
						return;
					}
				}
			}
		}else{
			$sql="SELECT n.*,m.user_name ,IFNULL(x.ws_tid,0) as ws_tid FROM zys_goods_normal n
		LEFT JOIN zys_member m ON n.g_optid=m.user_id
		LEFT JOIN (SELECT ws_tid,IFNULL(ws_gid,0) as ws_gid from zys_waybill_sub_normal GROUP BY ws_gid) x on n.g_tid=ws_gid WHERE g_tid=".$arr;
			$status=$db->getRow($sql);
			if($status&&$status['ws_tid']){
				$this->show_message('此货单不能删除' ,   '',   'index.php?app=goods_normal');
				return ;
			}else{
				if (!$this->_goodsnormal_mod->drop($arr)){
					$this->show_warning($this->_goodsnormal_mod->get_error() ,   '',   'index.php?app=goods_normal');
					return;
				}
			}
		}
		$this->show_message('删除成功',   '',   'index.php?app=goods_normal');
			
	}
	//加入到地址管理系统中

	function addaddr($data){
		$addr = & m('addr');
		$addr->add($data);
	}
	//验证地址是否已经存在
	function checkaddr($addr,$userid,$unit){
		$db = &db();
		$sql="select count(*) as num from zys_addr where address='".$addr."' and user_id=".$userid." and unit=".$unit;
		$num = $db->getone($sql);
		return $num;
	}
	//选中承运商
	function chooseop(){
		$db = &db();
		$type = $this->visitor->get('type');
		$unit = 0;
		if($type==4){
			$unit = $this->visitor->get('unit');
			$sql = "select o_name from zys_operator where o_carrier=1 and o_tid=".$unit;
			$o_name = $db->getOne($sql);
		}
		$this->assign('o_name', $o_name);
		$this->assign('unit', $unit);
		$page = $this->_get_page();

		//整车id
		$gtid=empty($_GET['gtid'])?0:intval($_GET['gtid']);


		$sql = "select gs.*,op.o_name,ws.ws_new from (zys_goods_normal_sub as gs left join zys_operator as op on gs.g_carrier = op.o_tid) left join zys_waybill_sub_normal as ws on ws.ws_gstid = gs.gs_tid where gs_gtid = ".$gtid ." group by gs.gs_tid";

		$goods = $this->_goodsnormalsub_mod->getAll($sql);

		$page['item_count'] = count($goods);

		$this->_format_page($page);
		$this->assign('page_info', $page);

		$this->assign('gs_gtid', $gtid);

		$this->assign('goods', $goods);

		$this->display('goods_normal.chooseop.html');
	}
	//处理承运商
	function opdone(){
		$db=&db();
		$checkarrs = isset($_POST['cnum']) ? $_POST['cnum'] : '';
		$gtid =isset($_POST['gs_gtid']) ? intval($_POST['gs_gtid']) : 0;
		$carrier =isset($_POST['g_carrier']) ? intval($_POST['g_carrier']) : 0;
		if(empty($carrier)){
			$this->show_warning('请选择承运商','','index.php?app=goods_normal&act=add&id='.$gtid);
			return;
		}
		if($checkarrs!=null&&count($checkarrs)>0){
			foreach ($checkarrs as $k=>$v){
				$sql="select * from zys_goods_normal_sub where gs_tid=".$k;
				$info=$db->getRow($sql);
				if(empty($info)){
					$this->show_warning('操作错误','','index.php?app=goods_normal&act=edit&id='.$gtid);
					return;
				}
				//指派
				$sql="UPDATE zys_goods_normal_sub SET g_carrier=".$carrier." WHERE gs_tid=$k";
				$db->query($sql);
				unset($info);
			}
		}
		echo("<script>window.parent.location.href='index.php?app=goods_normal&act=edit&id=".$gtid."';</script>");
	}

}